package com.ppz.controller;



import com.ppz.Service.UserService;
import com.ppz.dao.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


//接口
//描述文本 "User控制类"
@ApiOperation("User控制类")
@RestController
//处理HTTP GET请求
@RequestMapping("/UserInformation")
//用于对这个控制器类对应的 API 功能进行分组描述
@Api(tags = "手机号码服务|", description = "提供对于手机号码的服务")
//用于接受前端请求，调用相应服务层方法处理业务逻辑
public class UserController {

    @Autowired
    private UserService userService;

    //用户登录接口
    @GetMapping("/login")
    //放在方法上面
    @ApiOperation(value = "登录接口", notes = "用户登录")
    public Result loginUser(@RequestParam("phone") String phone, @RequestParam("password") String password) {
        String msg="手机号输入不正确";
        if (phone.length() != 11) {
            return new Result(Code.LOGIN_ERR,msg);
//                    "{Code: 500, msg:手机号输入不正确}";
        }
        for (int i = 0; i < phone.length(); i++) {
            if (phone.charAt(i) < '0' || phone.charAt(i) > '9') {
                return new Result(Code.LOGIN_ERR,msg);
                        //"{Code: 500, msg:手机号输入不正确}";
            }
        }
        User pExist = userService.isexistUser(phone);
        if (pExist != null) {
            User user = userService.loginUser(phone, password);
            if (user != null) {
                msg="登录成功";
                return new Result(Code.LOGIN_OK,msg);
//                        "{Code: 200, msg:登录成功}";
            }else {
                msg="登录失败，用户名不正确";
                return new Result(Code.LOGIN_ERR,msg);
//                        "{Code:500, msg:登录失败，用户名不正确}";
            }
        } else {
            msg="该手机号未注册";
            return  new Result(Code.LOGIN_ERR,msg);
//                    "{Code:500, msg:该手机号未注册}";
        }
    }

    //创建用户信息接口
    @PostMapping("/add")
    @ApiOperation(value = "注册接口", notes = "用户注册")
    public Result addUser(User user) {
        String msg="手机号输入不正确";
        if (user.getPhone().length() != 11) {
        return new Result(Code.ADD_OK,msg);
//                    "{Code: 500, msg:手机号输入不正确}";
        }
        for (int i = 0; i < user.getPhone().length(); i++) {
            if (user.getPhone().charAt(i) < '0' || user.getPhone().charAt(i) > '9') {
                return new Result(Code.ADD_ERR,msg);
//                        "{Code: 500, msg:手机号输入不正确}";
            }
        }
        User ur=userService.isexistUser(user.getPhone());
        if(ur!=null){
            msg="该手机好已经注册";
            return new Result(Code.ADD_ERR,msg);
                    //"{Code:500, msg:该号码已注册}";
        }else{
            userService.addUser(user);
            msg="注册成功";
            return new Result(Code.ADD_OK,msg);
                    //"{Code:200, msg:注册成功}";
        }

    }
    //修改用户信息接口
    @PutMapping("/update")
    @ApiOperation(value = "修改接口", notes = "修改用户信息")
    public Result updateUser(User user) {
        String msg="修改成功";
        User ur=userService.isexistUser(user.getPhone());
        if(ur!=null){
            userService.updateUser(user);

            return new Result(Code. UPDATE_OK,msg);
//                    "{Code:200, msg:修改成功}";
        }else{
             msg="修改成功";
            return  new Result(Code.UPDATE_ERR,msg);
//                    "{Code:500, msg:该用户不存在}";
        }
    }

    //获取用户信息
    @GetMapping("/getbyPhone")
    @ApiOperation(value = "查询接口", notes = "获取用户信息")
    public Result getByPhone(String phone) {
        String msg="手机号输入不正确";
        if (phone.length() != 11) {
            return new Result(Code.GET_ERR,msg);
//                    "{Code: 500, msg:手机号输入不正确}";
        }
        for (int i = 0; i < phone.length(); i++) {
            if (phone.charAt(i) < '0' || phone.charAt(i) > '9') {
                return new Result(Code.GET_ERR,msg);
//                        "{Code: 500, msg:手机号输入不正确}";
            }
        }
        User ur=userService.isexistUser(phone);
        if(ur!=null){
            msg="查询成功";
            return new Result(Code.GET_OK,ur,msg);
//                    "{Code:200, msg:查询成功,data:['phone:'"+ur.getPhone()+"" +
//                    "',username:'"+ur.getUsername()+"',password:'" +
//                    ""+ur.getPassword()+"',age:'"+ur.getAge()+"" +
//                    "',remark:'"+ur.getRemark()+"']}";
            }else{
            msg="该号码为空";
            return new Result(Code.GET_ERR,msg);
//                    "{Code:500, msg:该号码为空号}";
        }
    }

    //删除接口
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除接口", notes = "删除用户信息")
    public Result deleteUserByPhone(String phone) {
        boolean flag=userService.deleteUserByPhone(phone);
        if(flag){
            return new Result(Code.DELETE_OK,phone);
                    //"{成功删除:"+phone+"用户}";
        }else{
//            int i=1/0;
            return new Result(Code.DELETE_ERR,phone);
                    //"{删除:"+phone+"用户失败}";
        }
    }
}
